//
//  BRStackLabelView.m
//  Kexu
//
//  Created by IOS on 2023/7/17.
//

#import "BRStackLabelView.h"

@implementation BRStackLabelView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI --
- (void)setUI {
    self.axis = UILayoutConstraintAxisVertical;
    self.spacing = 10;
}


- (void)br_addSubLabel:(NSArray *)titles {
    
    [self br_addSubLabel:titles font:nil textColor:nil];
}

- (void)br_addSubLabel:(NSArray *)titles font:(UIFont * _Nullable)font textColor:(UIColor * _Nullable)color {
    
    if (!font) font = [UIFont fontM:12];
    if (!color) color = kColorTitle66B;
    
    for (int i = 0; i < titles.count; i++) {
        
        QMUILabel *label = [BRToolsView br_label:titles[i] textColor:color font:font];
        label.tag = 1000 + i;
        [self addArrangedSubview:label];
        
    }
}

- (void)br_text:(NSString *)text by:(NSInteger)index {
    QMUILabel *label = [self viewWithTag:1000 + index];
    label.text = text;
}

- (void)br_attText:(NSAttributedString *)attText by:(NSInteger)index {
    QMUILabel *label = [self viewWithTag:1000 + index];
    label.attributedText = attText;
}

- (void)br_textHidden:(BOOL)hidden by:(NSInteger)index {
    QMUILabel *label = [self viewWithTag:1000 + index];
    label.hidden = hidden;
}

@end

@implementation BRStackImgLabelView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI --
- (void)setUI {
    self.axis = UILayoutConstraintAxisVertical;
    self.spacing = 10;
}


- (void)br_addSubLabel:(NSArray *)titles {
    
    [self br_addSubLabel:titles font:nil textColor:nil];
}

- (void)br_addSubLabel:(NSArray *)titles font:(UIFont * _Nullable)font textColor:(UIColor * _Nullable)color {
    
    if (!font) font = [UIFont fontM:12];
    if (!color) color = kColorTitle66B;
    
    for (int i = 0; i < titles.count; i++) {
        
        BRImgLabelView *label = [[BRImgLabelView alloc] init];
        label.titleL.text = titles[i];
        label.titleL.font = font;
        label.titleL.textColor = color;
        label.tag = 1000 + i;
        [self addArrangedSubview:label];
        
    }
}

- (void)br_img:(NSString *)img by:(NSInteger)index {
    BRImgLabelView *label = [self viewWithTag:1000 + index];
    label.img.image = UIImageMake(img);
}

- (void)br_text:(NSString *)text by:(NSInteger)index {
    BRImgLabelView *label = [self viewWithTag:1000 + index];
    label.titleL.text = text;
}

- (void)br_attText:(NSAttributedString *)attText by:(NSInteger)index {
    BRImgLabelView *label = [self viewWithTag:1000 + index];
    label.titleL.attributedText = attText;
}

- (void)br_textHidden:(BOOL)hidden by:(NSInteger)index {
    BRImgLabelView *label = [self viewWithTag:1000 + index];
    label.hidden = hidden;
}

@end

@implementation BRStackCellView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)br_text:(NSString *)text by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.textL.text = text;
}

- (void)br_hidden:(BOOL)hidden by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.hidden = hidden;
}

- (void)br_hiddenLine:(BOOL)hidden by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.line.hidden = hidden;
}

- (void)br_textFont:(UIFont *)font by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.textL.font = font;
}

#pragma mark - UI --
- (void)setUI {
    
    self.axis = UILayoutConstraintAxisVertical;

}

- (void)br_addList:(NSArray *)titles {
    
    for (UIView *view in self.subviews) {
        [self removeArrangedSubview:view];
    }
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i = 0; i < titles.count; i++) {
        NSString *str = titles[i];
        if ([str isEqualToString:@"coupon_freeUsage"] || [str isEqualToString:@"order_discount"]) {
            BRODListImgView *cell = [[BRODListImgView alloc] init];
            [self addArrangedSubview:cell];
            cell.tag = 2000 + i;
            cell.titleL.text = str;
            continue;
        }
        BRODListView *cell = [[BRODListView alloc] init];
        cell.numLine = 2;
        [self addArrangedSubview:cell];
        cell.tag = 2000 + i;
        cell.titleL.text = str;
    }
}

@end


@implementation  BRODStackCellView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)br_text:(NSString *)text by:(NSInteger)index {

    [self br_title:@"" text:text by:index];
}

- (void)br_title:(NSString *)title text:(NSString *)text by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.textL.text = text;
    if (title.length != 0) {
        cell.titleL.text = title;
    }
}

- (void)br_hidden:(BOOL)hidden by:(NSInteger)index {
    BRODListView *cell = [self viewWithTag:2000 + index];
    cell.hidden = hidden;
}

- (void)br_img:(NSString *)img by:(NSInteger)index {
    BRODListImgView *cell = [self viewWithTag:2000 + index];
    cell.leftImg.image = UIImageMake(img);
}

#pragma mark - UI --
- (void)setUI {
    
    self.axis = UILayoutConstraintAxisVertical;

}

- (void)br_addList:(NSArray *)titles imgs:(NSArray *)imgs {
    
    for (UIView *view in self.subviews) {
        [self removeArrangedSubview:view];
    }
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i = 0; i < titles.count; i++) {
        NSString *str = titles[i];
        NSString *imgStr = @"";
        if (imgs.count != 0) {
            imgStr = imgs[i];
        }
        if (imgStr.length != 0) {
            BRODListImgView *cell = [[BRODListImgView alloc] init];
            [self addArrangedSubview:cell];
            cell.tag = 2000 + i;
            cell.titleL.text = str;
//            cell.leftImg
            cell.line.hidden = i == titles.count - 1;
            continue;
        }
        BRODListView *cell = [[BRODListView alloc] init];
        if ([str isEqualToString:Localized(@"pay_errorMsg")]) {
            
            cell.numLine = 0;;
        }
        if ([str isEqualToString:Localized(@"order_paymentMethod")]) {
            
            cell.numLine = 0;;
        }
        
        [self addArrangedSubview:cell];
        cell.tag = 2000 + i;
        cell.titleL.text = str;
        
        cell.line.hidden = i == titles.count - 1;
    }
}

@end

#pragma mark - BRMData2StackView --
@interface BRMData2StackView ()

@property (nonatomic,strong) NSArray *titles;
@end
@implementation BRMData2StackView

- (instancetype)initWhit:(NSArray *)titles
{
    self = [super init];
    if (self) {
        _titles = titles;
        [self setUI];
    }
    return self;
}

- (void)br_title:(NSString *)title text:(NSString *)text by:(NSInteger)index {
    BRMCustomTitle2View *titleView = [self viewWithTag:index + 300];
    titleView.stackView.title = title;
    titleView.stackView.text = text;
}
- (void)br_text:(NSString *)text by:(NSInteger)index {
    BRMCustomTitle2View *titleView = [self viewWithTag:index + 300];
    titleView.stackView.text = text;
}

- (void)br_showTips:(NSInteger)index block:(BRIDBlock)clickBlock {
    BRMCustomTitle2View *titleView = [self viewWithTag:index + 300];
    titleView.stackView.hasAttTitle = YES;
    titleView.stackView.titleBtnClick = clickBlock;
}


#pragma mark - UI --
- (void)setUI {
    
    self.spacing = 10;
    self.distribution = UIStackViewDistributionFillEqually;
    NSArray *titles = self.titles;
    for (int j = 0; j < titles.count; j++) {
        NSString *str  =  titles[j];
        
        BRMCustomTitle2View *titleView = [[BRMCustomTitle2View alloc] init];
        [titleView br_borderStyleDefault];
        [titleView br_leftAlignment];
        titleView.stackView.title = str;
        titleView.stackView.text = @"0";
        titleView.tag = j + 300;
        [self addArrangedSubview:titleView];
        
    }
}

// 取12字号计算
- (CGFloat)br_titleH {
    CGFloat h = 0;
    for (int i = 0; i < _titles.count; i++) {
        BRMCustomTitle2View *titleView = [self viewWithTag:300 + i];

        [titleView.stackView.titleL sizeToFit];
        CGSize sizeTitle = titleView.stackView.titleL.frame.size;

        if (h < sizeTitle.height) h = sizeTitle.height;
    }

    if (h > 16) return 84.; //标题2行
    
    return 70.; //1行
}

- (CGFloat)viewDefaultH {
    
    if (_viewDefaultH == 0) {
        
        _viewDefaultH = [self br_titleH];
        return _viewDefaultH;
    }
    
    return _viewDefaultH;
}

- (void)br_resizeDefaultH {
    _viewDefaultH = [self br_titleH];
}


@end


@implementation BRStackTitleTextView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI --
- (void)setUI {
    
    self.axis = UILayoutConstraintAxisVertical;
    self.spacing = 12;
    
    [self addArrangedSubview:self.titleL];
    [self addArrangedSubview:self.textL];
}


- (QMUILabel *)titleL {
    
    if (_titleL == nil) {
        _titleL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontR:12]];
        _titleL.textAlignment = NSTextAlignmentCenter;
    }
    return _titleL;
}

- (QMUILabel *)textL {
    if (_textL == nil) {
        _textL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontB:17]];
        _textL.textAlignment = NSTextAlignmentCenter;
    }
    return _textL;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleL.text = title;
}

- (void)setText:(NSString *)text {
    _text = text;
    self.textL.text = text;
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleL.textColor = titleColor;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.textL.textColor = textColor;
}

@end
